/*
 * Copyright (C) 2011 Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * pn_BinErr.java
 *
 * Created on 30/07/2011, 14:35:34
 */
package kuasar.plugin.deployer.gui.actions.dialogs;

import java.awt.Color;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 *
 * @author Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 */
public class pn_BinErr extends kuasar.plugin.classMod.AbstractPanel{
    
    private DefaultListModel model = new DefaultListModel();
    private boolean recheck = false;
    private boolean abort = false;
    
    /** Creates new form pn_BinErr */
    public pn_BinErr(HashMap<String,String[]> errors) {
        initComponents();
        loadData(errors);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbl_Title = new javax.swing.JLabel();
        lbl_Subtitle = new javax.swing.JLabel();
        pn_Data = new javax.swing.JPanel();
        spn_Errors = new javax.swing.JScrollPane();
        lst_Errors = new javax.swing.JList();
        spn_Marquee = new javax.swing.JScrollPane();
        lbl_Info = new kuasar.plugin.deployer.gui.classmod.JMarqueeLabel();
        btn_OK = new javax.swing.JButton();
        btn_Check = new javax.swing.JButton();
        btn_abort = new javax.swing.JButton();

        setOpaque(false);

        lbl_Title.setFont(new java.awt.Font("Dialog", 1, 24));
        lbl_Title.setForeground(new java.awt.Color(204, 204, 204));
        lbl_Title.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/deployer/icons/badbin.png"))); // NOI18N
        lbl_Title.setText("Error loading VM data");

        lbl_Subtitle.setForeground(new java.awt.Color(204, 204, 204));
        lbl_Subtitle.setText("There were problems loading next VM listed above");

        pn_Data.setOpaque(false);

        lst_Errors.setModel(model);
        lst_Errors.setCellRenderer(new ErrorCellRenderer());
        lst_Errors.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lst_ErrorsValueChanged(evt);
            }
        });
        spn_Errors.setViewportView(lst_Errors);

        spn_Marquee.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        spn_Marquee.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        spn_Marquee.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        spn_Marquee.setViewportBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        spn_Marquee.setOpaque(false);

        lbl_Info.setForeground(new java.awt.Color(204, 204, 204));
        lbl_Info.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        lbl_Info.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbl_InfoMouseClicked(evt);
            }
        });
        spn_Marquee.setViewportView(lbl_Info);
        lbl_Info.setVisible(false);

        javax.swing.GroupLayout pn_DataLayout = new javax.swing.GroupLayout(pn_Data);
        pn_Data.setLayout(pn_DataLayout);
        pn_DataLayout.setHorizontalGroup(
            pn_DataLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(spn_Errors, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE)
            .addGroup(pn_DataLayout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(spn_Marquee, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
                .addContainerGap())
        );
        pn_DataLayout.setVerticalGroup(
            pn_DataLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pn_DataLayout.createSequentialGroup()
                .addComponent(spn_Errors, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spn_Marquee, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        spn_Marquee.getViewport().setOpaque(false);

        btn_OK.setBackground(new java.awt.Color(0, 0, 0));
        btn_OK.setForeground(new java.awt.Color(204, 204, 204));
        btn_OK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/deployer/icons/ignore.png"))); // NOI18N
        btn_OK.setText("Ignore");
        btn_OK.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_OK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_OKActionPerformed(evt);
            }
        });

        btn_Check.setBackground(new java.awt.Color(0, 0, 0));
        btn_Check.setForeground(new java.awt.Color(204, 204, 204));
        btn_Check.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/deployer/icons/redo.png"))); // NOI18N
        btn_Check.setText("Check again");
        btn_Check.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_Check.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CheckActionPerformed(evt);
            }
        });

        btn_abort.setBackground(new java.awt.Color(0, 0, 0));
        btn_abort.setForeground(new java.awt.Color(204, 204, 204));
        btn_abort.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/deployer/icons/abort16.png"))); // NOI18N
        btn_abort.setText("Abort");
        btn_abort.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_abort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_abortActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(pn_Data, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(243, Short.MAX_VALUE)
                        .addComponent(btn_abort)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_Check)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_OK))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(lbl_Subtitle, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lbl_Title, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
                                .addGap(157, 157, 157)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(lbl_Title, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbl_Subtitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pn_Data, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_OK)
                    .addComponent(btn_Check)
                    .addComponent(btn_abort))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btn_CheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CheckActionPerformed
        answer(true);
    }//GEN-LAST:event_btn_CheckActionPerformed

    private void lst_ErrorsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lst_ErrorsValueChanged
        if(!evt.getValueIsAdjusting()){
            String[] data = (String[]) lst_Errors.getSelectedValue();
            lbl_Info.setText(data[1]);
            lbl_Info.setVisible(true);
        }         
    }//GEN-LAST:event_lst_ErrorsValueChanged

    private void lbl_InfoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbl_InfoMouseClicked
        if(evt.isControlDown()){
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            StringSelection string = new StringSelection(lbl_Info.getText());
            clipboard.setContents(string, null);
        }
    }//GEN-LAST:event_lbl_InfoMouseClicked

    private void btn_OKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_OKActionPerformed
        answer(false);
    }//GEN-LAST:event_btn_OKActionPerformed

private void btn_abortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_abortActionPerformed
        abort=true;
        answer(false);
}//GEN-LAST:event_btn_abortActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Check;
    private javax.swing.JButton btn_OK;
    private javax.swing.JButton btn_abort;
    private javax.swing.JLabel lbl_Info;
    private javax.swing.JLabel lbl_Subtitle;
    private javax.swing.JLabel lbl_Title;
    private javax.swing.JList lst_Errors;
    private javax.swing.JPanel pn_Data;
    private javax.swing.JScrollPane spn_Errors;
    private javax.swing.JScrollPane spn_Marquee;
    // End of variables declaration//GEN-END:variables

    private void answer(boolean recheck){
        this.recheck=recheck;
        WakeUp();
    }
    public synchronized void WaitAnswer(){
        try {
            wait();
        } catch (InterruptedException ex) {}
    }
    private synchronized void WakeUp(){
        notify();
    }
    private void loadData(HashMap<String, String[]> errors) {
        model.removeAllElements();
        Iterator<String> keys = errors.keySet().iterator();
        while(keys.hasNext()){
            String key = keys.next();
            String[] data = errors.get(key);
            model.addElement(new String[]{data[0],data[1], key});
        }
        
    }
    public boolean getAnswer(){
        return recheck;
    }
    public boolean getAbort(){
        return abort;
    }
    class ErrorCellRenderer extends JLabel implements ListCellRenderer{

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            /*VALUE => String[3] ::
             *                      [0] Name
             *                      [1] Error Description
             */
            String[] data = (String[]) value;
            String title = data[0];
            if(title.trim().isEmpty()){
                title = "<ERROR GETTING NAME. CLICK HERE FOR MORE INFO>";
            }
            JLabel label = new JLabel(title);
            label.setBackground(new Color(184,207,229));
            label.setOpaque(isSelected);
            return label;
        }
    
    }
}


